/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Consultar.java
 *
 * Created on 02/11/2011, 12:00:14
 */
package Interfaces;

import classes.Atividade;
import classes.Minicurso;
import classes.Painel;
import classes.Palestra;
import classes.Workshop;
import conexaoBD.mySQLcrud;
import java.awt.geom.Dimension2D;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kazuo
 */
public class TelaConsulta extends javax.swing.JFrame {

    /** Creates new form Consultar */
    private mySQLcrud connectionCrud;

    public TelaConsulta() {
        initComponents();
        connectionCrud = new mySQLcrud();
        jTableDinamica = new javax.swing.JTable();

        jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{ //"ID", "Nome", "Descrição"
                }));
        jScrollPane1.setViewportView(jTableDinamica);

        lblParamConsulta.setText("Dados");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanelTitulo = new javax.swing.JPanel();
        lblTitulo = new javax.swing.JLabel();
        lblParamConsulta = new javax.swing.JLabel();
        jPanelSelecao = new javax.swing.JPanel();
        rbMiniCurso = new javax.swing.JRadioButton();
        rbWorkshop = new javax.swing.JRadioButton();
        rbPalestra = new javax.swing.JRadioButton();
        rbPainel = new javax.swing.JRadioButton();
        jPanelBuscaTexto = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtPesquisa = new javax.swing.JTextField();
        btDetalhes = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        scrollPane2 = new java.awt.ScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(3, 1));

        lblTitulo.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblTitulo.setText("Consulta de ");
        jPanelTitulo.add(lblTitulo);

        lblParamConsulta.setFont(new java.awt.Font("Tahoma", 1, 16));
        lblParamConsulta.setText("PARAM_CONSULTA");
        jPanelTitulo.add(lblParamConsulta);

        jPanel1.add(jPanelTitulo);

        buttonGroup1.add(rbMiniCurso);
        rbMiniCurso.setText("Mini-Curso");
        rbMiniCurso.setToolTipText("Selecione a opção para a consulta.");
        rbMiniCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbMiniCursoActionPerformed(evt);
            }
        });
        jPanelSelecao.add(rbMiniCurso);

        buttonGroup1.add(rbWorkshop);
        rbWorkshop.setText("Workshop");
        rbWorkshop.setToolTipText("Selecione a opção para a consulta.");
        rbWorkshop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbWorkshopActionPerformed(evt);
            }
        });
        jPanelSelecao.add(rbWorkshop);

        buttonGroup1.add(rbPalestra);
        rbPalestra.setText("Palestra");
        rbPalestra.setToolTipText("Selecione a opção para a consulta.");
        rbPalestra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPalestraActionPerformed(evt);
            }
        });
        jPanelSelecao.add(rbPalestra);

        buttonGroup1.add(rbPainel);
        rbPainel.setText("Painel");
        rbPainel.setToolTipText("Selecione a opção para a consulta.");
        rbPainel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPainelActionPerformed(evt);
            }
        });
        jPanelSelecao.add(rbPainel);

        jPanel1.add(jPanelSelecao);

        jPanelBuscaTexto.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText(" Filtrar por:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanelBuscaTexto.add(jLabel1, gridBagConstraints);

        txtPesquisa.setToolTipText("Digite o filtro (Todas as opções serão exibidas caso esteja vazio)");
        txtPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPesquisaKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.ipadx = 170;
        jPanelBuscaTexto.add(txtPesquisa, gridBagConstraints);

        btDetalhes.setText("Mostrar Detalhes");
        btDetalhes.setToolTipText("Clique aqui para consultar");
        btDetalhes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDetalhesActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanelBuscaTexto.add(btDetalhes, gridBagConstraints);

        jPanel1.add(jPanelBuscaTexto);

        scrollPane2.add(jScrollPane1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 972, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 972, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btDetalhesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDetalhesActionPerformed

        if (jTableDinamica.getSelectedRow() >= 0) {
            String codigo = jTableDinamica.getValueAt(jTableDinamica.getSelectedRow(), 0).toString();

            if (rbMiniCurso.isSelected()) {
                exibirMiniCurso(codigo);
            }
           //a
            if (rbPainel.isSelected()) {
                exibirPainel(codigo);
            }
            if (rbPalestra.isSelected()) {
                exibirPalestra(codigo);
            }
            if (rbWorkshop.isSelected()) {
            }
        } else {
            if (rbMiniCurso.isSelected() || rbPainel.isSelected() || rbPalestra.isSelected() || rbWorkshop.isSelected()) {
                JOptionPane.showMessageDialog(null, "Uma linha da tabela deve estar selecionada.");
            } else {
                JOptionPane.showMessageDialog(null, "Selecione uma opção para ser exibida.");
            }

        }
    }//GEN-LAST:event_btDetalhesActionPerformed

    private void rbMiniCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbMiniCursoActionPerformed
        txtPesquisa.setText("");
        ArrayList<Minicurso> mc = new ArrayList<Minicurso>();
        lblParamConsulta.setText("Minicurso");
        jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{"ID", "Nome", "Descrição"
                }));
        mc = connectionCrud.listaMinicurso(txtPesquisa.getText().trim());

        preencheTabelaMinicurso(mc);
    }//GEN-LAST:event_rbMiniCursoActionPerformed

    private void rbWorkshopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbWorkshopActionPerformed
        lblParamConsulta.setText("Workshop");
        jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "ID", "NAO DEFINIDO", "NAO DEFINIDO"
                }));
    }//GEN-LAST:event_rbWorkshopActionPerformed

    private void rbPalestraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPalestraActionPerformed
        txtPesquisa.setText("");
        ArrayList<Palestra> pn = new ArrayList<Palestra>();
        lblParamConsulta.setText("Palestra");
        jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "ID", "Título", "Resumo"
                }));
        pn = connectionCrud.listaPalestra(txtPesquisa.getText().trim());
        preencheTabelaPalestra(pn);
    }//GEN-LAST:event_rbPalestraActionPerformed

    private void rbPainelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPainelActionPerformed
        txtPesquisa.setText("");
        ArrayList<Painel> pn = new ArrayList<Painel>();
        lblParamConsulta.setText("Painel");
        jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "ID", "Nome do Moderador", "Instituição do Moderador"
                }));
        pn = connectionCrud.listaPainel(txtPesquisa.getText().trim());
        preencheTabelaPainel(pn);
    }//GEN-LAST:event_rbPainelActionPerformed

    private void txtPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPesquisaKeyReleased
        // TODO add your handling code here:
        ArrayList<Minicurso> mc = new ArrayList<Minicurso>();
        ArrayList<Painel> pn = new ArrayList<Painel>();
        ArrayList<Palestra> pl = new ArrayList<Palestra>();
        ArrayList<Workshop> wr = new ArrayList<Workshop>();

        if (rbMiniCurso.isSelected()) {
            jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{},
                    new String[]{
                        "ID", "Nome", "Descrição"
                    }));
            mc = connectionCrud.listaMinicurso(txtPesquisa.getText().trim());
            preencheTabelaMinicurso(mc);
        }
        if (rbPainel.isSelected()) {
            jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{},
                    new String[]{
                        "ID", "Nome do Moderador", "Instituição do Moderador"
                    }));
            pn = connectionCrud.listaPainel(txtPesquisa.getText().trim());
            preencheTabelaPainel(pn);
        }
        if (rbPalestra.isSelected()) {
            jTableDinamica.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{},
                    new String[]{
                        "ID", "Título", "Resumo"
                    }));
            pl = connectionCrud.listaPalestra(txtPesquisa.getText().trim());
            preencheTabelaPalestra(pl);
        }
        if (rbWorkshop.isSelected()) {
            preencheTabelaWorkshop(mc);
        }
    }//GEN-LAST:event_txtPesquisaKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaConsulta().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDetalhes;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelBuscaTexto;
    private javax.swing.JPanel jPanelSelecao;
    private javax.swing.JPanel jPanelTitulo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblParamConsulta;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JRadioButton rbMiniCurso;
    private javax.swing.JRadioButton rbPainel;
    private javax.swing.JRadioButton rbPalestra;
    private javax.swing.JRadioButton rbWorkshop;
    private java.awt.ScrollPane scrollPane2;
    private javax.swing.JTextField txtPesquisa;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JTable jTableDinamica;

    private void preencheTabelaPainel(ArrayList<Painel> listaParam) {
        ArrayList<Painel> listaPainel = new ArrayList<Painel>();
        for (Object o : listaParam) {
            listaPainel.add((Painel) o);
        }
        for (int i = 0; i < listaParam.size(); i++) {
            ArrayList<Object> retornaTabela = new ArrayList<Object>();
            retornaTabela.add(listaPainel.get(i).getCodPainel());
            retornaTabela.add(listaPainel.get(i).getNomeModerador());
            retornaTabela.add(listaPainel.get(i).getInstituicaoModerador());
            ((DefaultTableModel) jTableDinamica.getModel()).addRow(new Vector(retornaTabela));
        }
    }

    private void preencheTabelaPalestra(ArrayList<Palestra> listaParam) {
        ArrayList<Palestra> listaPalestras = new ArrayList<Palestra>();
        for (Object o : listaParam) {
            listaPalestras.add((Palestra) o);
        }
        for (int i = 0; i < listaParam.size(); i++) {
            ArrayList<Object> retornaTabela = new ArrayList<Object>();
            retornaTabela.add(listaPalestras.get(i).getCodPalestra());
            retornaTabela.add(listaPalestras.get(i).getAtividade().getTituloAtividade());
            retornaTabela.add(listaPalestras.get(i).getResumoPalestra());
            ((DefaultTableModel) jTableDinamica.getModel()).addRow(new Vector(retornaTabela));
        }
    }

    private void preencheTabelaWorkshop(ArrayList<Minicurso> listaParam) {
        ArrayList<Minicurso> listaWorkshop = new ArrayList<Minicurso>();

        for (Object o : listaParam) {
            listaWorkshop.add((Minicurso) o);
        }
        for (int i = 0; i < listaParam.size(); i++) {
            //ArrayList<Object> retornaTabela = new ArrayList<Object>();
            //retornaTabela.add(listaWorkshop.get(i).getCodMinicurso());
            //retornaTabela.add(listaWorkshop.get(i).getNomeMinicurso());
            //retornaTabela.add(listaWorkshop.get(i).getDescricaoMinicurso());
            //((DefaultTableModel) jTableDinamica.getModel()).addRow(new Vector(retornaTabela));
        }
    }

    private void preencheTabelaMinicurso(ArrayList<Minicurso> listaParam) {
        ArrayList<Minicurso> listaMiniCurso = new ArrayList<Minicurso>();

        for (Object o : listaParam) {
            listaMiniCurso.add((Minicurso) o);
        }
        for (int i = 0; i < listaParam.size(); i++) {
            ArrayList<Object> retornaTabela = new ArrayList<Object>();

            retornaTabela.add(listaMiniCurso.get(i).getCodMinicurso());
            retornaTabela.add(listaMiniCurso.get(i).getNomeMinicurso());
            retornaTabela.add(listaMiniCurso.get(i).getDescricaoMinicurso());
            ((DefaultTableModel) jTableDinamica.getModel()).addRow(new Vector(retornaTabela));
        }
    }

    private void exibirMiniCurso(String codigo) {
        
        new TelaExibicaoMiniCurso(codigo).setVisible(true);
    }

    private void exibirPainel(String codigo) {
        
        new TelaExibicaoAtividadePainel(codigo).setVisible(true);
    }

    private void exibirPalestra(String codigo) {
        
        new TelaExibicaoAtividadePalestra(codigo).setVisible(true);
    }
}
